package club.emergency.wechat.dictionary.operate.provider.impl;

import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.dictionary.operate.DictionaryOperate;
import club.emergency.wechat.model.dictionary.DictionaryChild;
import club.emergency.wechat.model.dictionary.DictionaryParent;
import club.emergency.wechat.persistence.dictionary.DictionaryChildMapper;
import club.emergency.wechat.persistence.dictionary.DictionaryParentMapper;
import club.emergency.wechat.redis.RedisOperation;
import club.emergency.wechat.redis.common.RedisKeyConstant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: business
 * Package Name: club.emergency.safetytraining.dictionary.operate.provider.impl
 * Date: 2018/11/26
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
@Transactional(rollbackFor = Exception.class)
public class DictionaryOperateImpl implements DictionaryOperate {

    private DictionaryParentMapper dictionaryParentMapper;

    private DictionaryChildMapper dictionaryChildMapper;

    private RedisOperation<DictionaryChild> redisOperation;

    public DictionaryOperateImpl(
            DictionaryParentMapper dictionaryParentMapper,
            DictionaryChildMapper dictionaryChildMapper,
            RedisOperation<DictionaryChild> redisOperation
    ) {
        this.dictionaryParentMapper = dictionaryParentMapper;
        this.dictionaryChildMapper = dictionaryChildMapper;
        this.redisOperation = redisOperation;
    }

    @Override
    public String saveParent(@RequestBody DictionaryParent dictionaryParent) {
        //查询keyName是否重复
        QueryWrapper<DictionaryParent> keyNameWrapper = new QueryWrapper<>();
        keyNameWrapper.eq("key_name", dictionaryParent.getKeyName());
        Integer keyNameCount = dictionaryParentMapper.selectCount(keyNameWrapper);
        //查询名称是否重复
        QueryWrapper<DictionaryParent> contentWrapper = new QueryWrapper<>();
        contentWrapper.eq("name", dictionaryParent.getName());
        Integer nameCount = dictionaryParentMapper.selectCount(contentWrapper);
        if (!keyNameCount.equals(0)) {
            return ResponseEntity.error(StatusCode.DB_SAVE_ERROR, dictionaryParent.getKeyName() + "关键字在父字典表已存在!");
        }
        if (!nameCount.equals(0)) {
            return ResponseEntity.error(StatusCode.DB_SAVE_ERROR, dictionaryParent.getName() + "名称在父字典表已存在!");
        }
        dictionaryParentMapper.insert(dictionaryParent);
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "父字典数据新增成功!", dictionaryParent.getId());
    }

    @Override
    public String modifyParent(@RequestBody DictionaryParent dictionaryParent) {
        int i = dictionaryParentMapper.updateById(dictionaryParent);
        if (i == 0) {
            return ResponseEntity.error(StatusCode.DB_MODIFY_ERROR, "父字典数据修改错误！");
        }
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "父字典数据修改成功！");
    }

    @Override
    public String removeParent(@RequestParam("id") Long id) {
        //先判断子字典表是否有parent_id为id的值
        QueryWrapper<DictionaryChild> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        Integer count = dictionaryChildMapper.selectCount(queryWrapper);
        if (Objects.nonNull(count) && !count.equals(0)) {
            return ResponseEntity.error(StatusCode.DB_REMOVE_ERROR, "请先删除子字典parent_id为传入的id值得数据之后在尝试删除该条记录", null);
        } else {
            dictionaryParentMapper.deleteById(id);
            return ResponseEntity.ok(StatusCode.DB_REMOVE_OK, "父字典数据删除成功!");
        }
    }

    @Override
    public String saveChild(@RequestBody DictionaryChild dictionaryChild) {
        //查询名称是否重复
        QueryWrapper<DictionaryChild> contentWrapper = new QueryWrapper<>();
        contentWrapper.eq("content", dictionaryChild.getContent());
        Integer contentCount = dictionaryChildMapper.selectCount(contentWrapper);
        if (!contentCount.equals(0)) {
            return ResponseEntity.error(StatusCode.DB_SAVE_ERROR, dictionaryChild.getContent() + "名称在子字典表已存在!");
        }
        dictionaryChildMapper.insert(dictionaryChild);
        //删除缓存,以便更新
        String key = RedisKeyConstant.DICTIONARY_CHILD + dictionaryChild.getKeyName();
        if (redisOperation.hasKey(key)) {
            redisOperation.delete(key);
        }
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "子字典数据新增成功!", dictionaryChild.getId());
    }

    @Override
    public String modifyChild(@RequestBody DictionaryChild dictionaryChild, @RequestParam("keyName") String keyName) {
        dictionaryChildMapper.updateById(dictionaryChild);
        //删除缓存,以便更新
        String key = RedisKeyConstant.DICTIONARY_CHILD + keyName;
        if (redisOperation.hasKey(key)) {
            redisOperation.delete(key);
        }
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "子字典数据修改成功！");
    }

}
